home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.4)
-
- from test.test_support import TestFailed, TESTFN, unlink
- from types import ClassType
- import warnings
- import sys
- import traceback
- import os
- print '5. Built-in exceptions'
-
- try:
- import exceptions
- reload(exceptions)
- except ImportError:
- e = None
- raise TestFailed, e
-
-
- def test_raise_catch(exc):
-
- try:
- raise exc, 'spam'
- except exc:
- err = None
- buf = str(err)
-
-
- try:
- raise exc('spam')
- except exc:
- err = None
- buf = str(err)
-
- print buf
-
-
- def r(thing):
- test_raise_catch(thing)
- if isinstance(thing, ClassType):
- print thing.__name__
- else:
- print thing
-
- r(AttributeError)
- import sys
-
- try:
- x = sys.undefined_attribute
- except AttributeError:
- pass
-
- r(EOFError)
- import sys
- fp = open(TESTFN, 'w')
- fp.close()
- fp = open(TESTFN, 'r')
- savestdin = sys.stdin
-
- try:
- sys.stdin = fp
- x = raw_input()
- except EOFError:
- pass
- finally:
- sys.stdin = savestdin
- fp.close()
-
- r(IOError)
-
- try:
- open('this file does not exist', 'r')
- except IOError:
- pass
-
- r(ImportError)
-
- try:
- import undefined_module
- except ImportError:
- pass
-
- r(IndexError)
- x = []
-
- try:
- a = x[10]
- except IndexError:
- pass
-
- r(KeyError)
- x = { }
-
- try:
- a = x['key']
- except KeyError:
- pass
-
- r(KeyboardInterrupt)
- print '(not testable in a script)'
- r(MemoryError)
- print '(not safe to test)'
- r(NameError)
-
- try:
- x = undefined_variable
- except NameError:
- pass
-
- r(OverflowError)
- warnings.filterwarnings('error', '', OverflowWarning, __name__)
- x = 1
- for dummy in range(128):
- x += x
-
- r(RuntimeError)
- print '(not used any more?)'
- r(SyntaxError)
-
- try:
- exec '/\n'
- except SyntaxError:
- pass
-
-
- def ckmsg(src, msg):
-
- try:
- compile(src, '<fragment>', 'exec')
- except SyntaxError:
- e = None
- print e.msg
- if e.msg == msg:
- print 'ok'
- else:
- print 'expected:', msg
- except:
- e.msg == msg
-
- print 'failed to get expected SyntaxError'
-
- s = 'while 1:\n try:\n pass\n finally:\n continue\n'
- if sys.platform.startswith('java'):
- print "'continue' not supported inside 'finally' clause"
- print 'ok'
- else:
- ckmsg(s, "'continue' not supported inside 'finally' clause")
- s = 'try:\n continue\nexcept:\n pass\n'
- ckmsg(s, "'continue' not properly in loop")
- ckmsg('continue\n', "'continue' not properly in loop")
- r(IndentationError)
- r(TabError)
- r(SystemError)
- print '(hard to reproduce)'
- r(SystemExit)
- import sys
-
- try:
- sys.exit(0)
- except SystemExit:
- pass
-
- r(TypeError)
-
- try:
- [] + ()
- except TypeError:
- pass
-
- r(ValueError)
-
- try:
- x = chr(10000)
- except ValueError:
- pass
-
- r(ZeroDivisionError)
-
- try:
- x = 1 / 0
- except ZeroDivisionError:
- pass
-
- r(Exception)
-
- try:
- x = 1 / 0
- except Exception:
- e = None
-
-
- class BadException:
-
- def __init__(self):
- raise RuntimeError, "can't instantiate BadException"
-
-
-
- def test_capi1():
- import _testcapi
-
- try:
- _testcapi.raise_exception(BadException, 1)
- except TypeError:
- err = None
- (exc, err, tb) = sys.exc_info()
- co = tb.tb_frame.f_code
- if not co.co_name == 'test_capi1':
- raise AssertionError
- if not co.co_filename.endswith('test_exceptions' + os.extsep + 'py'):
- raise AssertionError
-
- print 'Expected exception'
-
-
- def test_capi2():
- import _testcapi
-
- try:
- _testcapi.raise_exception(BadException, 0)
- except RuntimeError:
- err = None
- (exc, err, tb) = sys.exc_info()
- co = tb.tb_frame.f_code
- if not co.co_name == '__init__':
- raise AssertionError
- if not co.co_filename.endswith('test_exceptions' + os.extsep + 'py'):
- raise AssertionError
- co2 = tb.tb_frame.f_back.f_code
- if not co2.co_name == 'test_capi2':
- raise AssertionError
-
- print 'Expected exception'
-
- if not sys.platform.startswith('java'):
- test_capi1()
- test_capi2()
-
- unlink(TESTFN)
-